﻿Ext.define('app.prm.project.SummaryView', {
    extend: 'Ext.panel.Panel',
    border: false,
    bodyStyle: 'padding:3px',
    layout: 'border',
    filter: { WeekTime: new Date(), WeekType: 'Now' },
    initComponent: function () {
        var me = this;
        me.items = [
            {
                region: 'north',
                height: 60,
                bodyStyle: 'padding:10px',
                border: false,
                layout: 'fit',
                items: [
                    me.getRdCenter()
                ]
            },
            me.getCenter()
        ];
        me.callParent();
    },
    listeners: {
        afterrender: function (me) {
            App.AjaxGet('/api/services/app/rdCenter/GetAll', function (data) {
                console.log(data);
                for (var i = 0; i < data.result.length; i++) {
                    var record = data.result[i];
                    var radio = new Ext.form.field.Radio({ boxLabel: record.name, checked: record.id == 1, name: 'rdCenter', inputValue: record.id });
                    me.getRdCenter().add(radio);
                }

                me.updateWeekTitle('Now');
            });
        }
    },
    getCenter: function () {
        var me = this;
        if (!this.summaryCenter) {
            this.summaryCenter = new Ext.Panel({
                region: 'center',
                title: 'xxxx',
                autoScroll: true,
                tbar: [
                    { text: '上一周', glyph: 0xf100, handler: function () { me.updateWeekTitle('上一周'); } },
                    { text: '下一周', glyph: 0xf101, handler: function () { me.updateWeekTitle('下一周'); } }
                ],
                border: false,
                items: []
            });
        }
        return this.summaryCenter;
    },
    getRdCenter: function () {
        var me = this;
        if (!this.rdCenter) {
            this.rdCenter = new Ext.form.RadioGroup(
            {
                fieldLabel: '研发中心',
                columns: 5,
                items: [],
                listeners: {
                    change: function (chk, newValue, oldValue, eOpts) {
                        var rdCenterId = newValue.rdCenter;
                        me.filter.WeekTime = new Date();
                        me.updateWeekTitle('Now');
                    }
                }
            });
        }
        return this.rdCenter;
    },
    updateWeekTitle: function (weekType) {
        var me = this;
        me.filter.WeekType = weekType;
        App.AjaxPost('/api/services/app/summary/GetYearAndWeek', me.filter, function (data) {
            var record = data.result;
            me.filter.WeekTime = record.startTime;
            me.getCenter().setTitle(record.year + "年" + record.week + "周" + "&nbsp;(" + record.startTime + "&nbsp;~&nbsp;" + record.endTime + ")");

            me.getCenter().removeAll(true);
            var rdCenterId = me.getRdCenter().getChecked()[0].inputValue;
            App.AjaxGet('/api/services/app/laboratory/GetByCenterId?rdCenterId=' + rdCenterId, function (data) {
                console.log(data);
                for (var i = 0; i < data.result.length; i++) {
                    var rec = data.result[i];
                    var panel = Ext.create('app.prm.project.SummaryShow', { laboratoryId: rec.id, startTime: record.startTime });
                    me.getCenter().add(panel);
                }
            });


        });
    }
})